package com.azkj.album.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.azkj.album.R;
import com.azkj.album.adapter.ListAdapter;
import com.azkj.album.dto.ListData;
import com.azkj.album.dto.MessageEvent;
import com.azkj.album.dto.MessageType;
import com.azkj.album.network.ConfigStorage;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.ServiceCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.view.base.BaseActivity;
import com.azkj.album.view.widgets.dialog.DialogHelper;
import com.azkj.album.view.widgets.staggedrv.GridItemDecoration;
import com.azkj.album.view.widgets.staggedrv.StaggerdRecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainCollectActivity extends BaseActivity {

    @BindView(R.id.tv_search)
    TextView mTvSearch;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.rv_main)
    StaggerdRecyclerView mRecyclerView;

    @BindView(R.id.tv_finish)
    TextView tvFinish;
    private ListAdapter mAdapter;
    private int notifyIndex = -1;

    private int mPage = 1;
    private String keyword = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collect;
    }

    @Override
    public void initView() {
        mAdapter = new ListAdapter();
        mRecyclerView.link(mAdapter, 2);
        mAdapter.bindToRecyclerView(mRecyclerView.rv);
        mAdapter.setEmptyView(R.layout.list_empty);
        mRecyclerView.addDecoration(new GridItemDecoration(this, 10));
        RecyclerView.ItemAnimator animator = mRecyclerView.rv.getItemAnimator();
        if (animator instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
        }
        // 第二种，设置动画时间为0
        mRecyclerView.rv.getItemAnimator().setChangeDuration(0);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ListData item = mAdapter.getData().get(position);
                notifyIndex = position;
                Intent intent = new Intent(MainCollectActivity.this, DetailActivity.class);
                intent.putExtra("detail", item);
                intent.putExtra("fromType", DetailActivity.FROM_TYPE_COLLECT);
                startActivity(intent);
            }
        });
        mRefreshLayout.setEnableAutoLoadMore(false);
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPage++;
                getDataList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPage = 1;
                getDataList();
            }
        });
    }

    @Override
    public void initData() {
        getDataList();
    }

    @Override
    protected void initPresenter() {

    }

    @OnClick({R.id.tv_search, R.id.iv_back})
    void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.tv_search:
                Intent intent = new Intent(this, SearchActivity.class);
                intent.putExtra("pageType", 2);
                startActivity(new Intent(intent));
                break;


        }

    }

    public void getDataList() {
        NetworkMaster.getInstance().getCommonService().mainCollectList(keyword,
                String.valueOf(mPage), "", "", new ServiceCallback<BaseResp<List<ListData>>>() {
                    @Override
                    public void onSuccess(BaseResp data) {
                        DialogHelper.hideDialog();
                        mRefreshLayout.finishLoadMore(true);
                        mRefreshLayout.finishRefresh(true);
                        if (data.getCode() == 1) {
                            List<ListData> list = (List<ListData>) data.getData();
                            if (mPage == 1) {
                                mAdapter.setNewData(list);
                                if (!list.isEmpty()) {
                                    mTvSearch.setVisibility(View.VISIBLE);
                                }
                            } else {
                                mAdapter.addData(list);
                            }
                            //处理数据
                            if (list.size() < 10) {
                                tvFinish.setVisibility(View.VISIBLE);
                                mRefreshLayout.setEnableLoadMore(false);
                            } else {
                                tvFinish.setVisibility(View.GONE);
                                mRefreshLayout.setEnableLoadMore(true);
                            }
                        } else {
                            ToastUtils.showCenterToast(data.getMsg());
                        }
                    }

                    @Override
                    public void onError(ApiException apiException) {
                        DialogHelper.hideDialog();
                        mRefreshLayout.finishLoadMore(true);
                        mRefreshLayout.finishRefresh(true);
                        ToastUtils.showCenterToast(apiException.errorInfo.error);
                    }

                });
    }

    @Subscribe
    public void onMessage(MessageEvent messageEvent) {
        super.onMessage(messageEvent);
        switch (messageEvent.getType()) {
            case MessageType.TYPE_ON_DETAIL_DEL:
                if (null != mAdapter && -1 != notifyIndex) {
                    mAdapter.remove(notifyIndex);
                    notifyIndex = -1;
                }
                break;
            case MessageType.TYPE_ON_COLLECT_CANCEL:
                mPage = 1;
                getDataList();
                break;
        }

    }

}
